{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1595339539603",
   "display_name": "Python 3.7.4 64-bit ('tensorflow': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 高级操作\n",
    "import tensorflow as tf\n",
    "a = tf.random.normal([3,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = a>0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(6,), dtype=float32, numpy=\narray([0.57997155, 0.03704736, 0.02907122, 0.2779562 , 0.80068433,\n       1.0805314 ], dtype=float32)>"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "tf.boolean_mask(a,mask)  # 取True的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "indices = tf.where(mask)  # 获取True的坐标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(6,), dtype=float32, numpy=\narray([0.57997155, 0.03704736, 0.02907122, 0.2779562 , 0.80068433,\n       1.0805314 ], dtype=float32)>"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "tf.gather_nd(a,indices)  # 效果同Boolean_mask()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "indices = tf.constant([[4],[3],[1],[7]])\n",
    "updates = tf.constant([9,10,11,12])\n",
    "shape = tf.constant([8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<tf.Tensor: shape=(8,), dtype=int32, numpy=array([ 0, 11,  0, 10,  9,  0,  0, 12])>"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "tf.scatter_nd(indices,updates,shape)  # 根据指定位置进行更新"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}